<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class MalController extends Controller{
	public function actionIndex(){

		return $this->renderPartial('index');
	}
	public function actionPage(){
		// 创建一个 DB 查询来获得所有 status 为 1 的文章
		$query = Article::find()->where(['status' => 1]);

		// 得到文章的总数（但是还没有从数据库取数据）
		$count = $query->count();

		// 使用总数来创建一个分页对象
		$pagination = new Pagination(['totalCount' => $count]);

		// 使用分页对象来填充 limit 子句并取得文章数据
		$articles = $query->offset($pagination->offset)
		    ->limit($pagination->limit)
 		   ->all();
	}
	public function actionCheck(){
				$post = Yii::$app->request->post();
		if(Yii::$app->request->isPost){
			$sql = "SELECT * FROM users WHERE user_name='{$post['user_name']}' AND is_deleted=0";
			$data = Yii::$app->db->createCommand($sql)->queryOne();
			if(empty($data)){
				//json

				echo "对不起，你输入的用户名不存在";exit;
			}else{
				$pwd = md5($post['pwd']);
				$sql = "SELECT * FROM marathon_registration WHERE name='{$post['name']}' AND pwd='{$pwd}' AND is_deleted=0";
				$data = Yii::$app->db->createCommand($sql)->queryOne();
				if(empty($data)){
					echo "对不起，你输入的密码不对";exit;
				}else{
					//登录成功
					$session = Yii::$app->session;
					//处理登录数据
					$login_data = [
						'id' => $data['id'],
						'name' => $data['name'],
						'phone' => $data['phone'],
					];
					$session->set('userinfo', $login_data);
					echo json_encode(['code'=>200,'msg'=>'登录成功']);exit;
					echo "200";exit;
				}
			}
		}
	}
		public function actionlogin(){
		$session = Yii::$app->session;
		$userinfo = $session->get('userinfo');
		if(empty($userinfo)){
			$this->redirect('/index.php?r=login/index',301);
		}
	}
		public function actionLoginout(){
		$session = Yii::$app->session;
		$userinfo = $session->set('userinfo','');
		if(empty($userinfo)){
			$this->redirect('/index.php?r=login/index',301);
		}
	}
}